﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;

/// <summary>
/// Summary description for DataProvider
/// </summary>

public class DataProvider
{
    public const int GRID_NO = 30;

    public GridResult GetExamenData(int page, string name)
    {
        var adapter = new DataSetTableAdapters.Func_ExamenTableAdapter();
        var table = new DataSet.Func_ExamenDataTable();
        adapter.Fill(table, 12, 25, true);
        var rows = table.Where(x => x.Asignaturas.ToLower().Contains(name)).Skip((page - 1) * GRID_NO).Take(GRID_NO)
            .Select(x => new ExamenDTO
            {
                Asignaturas = x.Asignaturas,
                IDAsignatura = x.IDAsignatura,
                IExamenI = x.IExamenI,
                IExamenII = x.IExamenII,
                IExamenIII = x.IExamenIII,
                IIExamenI = x.IIExamenI,
                IIExamenII = x.IIExamenII,
                IIExamenIII = x.IIExamenIII,
                IExamenIV = x.IExamenIV,
                IIExamenIV = x.IIExamenIV,
                CPFS_I = x.CPFS_I,
                CPFS_II = x.CPFS_II,
                CFS_I = x.CFS_I,
                CFS_II = x.CFS_II,
                CF = x.CF,
                PCP = x.PCP,
                PCP70 = x.PCP70,
                CPFS = x.CPFS,
                CPSF30 = x.CPSF30,
                CFS = x.CFS,
                IPCP50 = x.IPCP50,
                ICPC_Completiva = x.ICPC_Completiva,
                ICPC50 = x.ICPC50,
                ICC = x.ICC,
                IPCP30 = x.IPCP30,
                ICPEX_Extraordinaria = x.ICPEX_Extraordinaria,
                ICPEX70 = x.ICPEX70,
                ICEX = x.ICEX,
                IIPCP = x.IIPCP,
                IIPCP70 = x.IIPCP70,
                IICPFS = x.IICPFS,
                IICPSF30 = x.IICPSF30,
                IICFS = x.IICFS,
                IIPCP50 = x.IIPCP50,
                IICPC_Completiva = x.IICPC_Completiva,
                IICPC50 = x.IICPC50,
                IICC = x.IICC,
                IIPCP30 = x.IIPCP30,
                IICPEX_Extraordinaria = x.IICPEX_Extraordinaria,
                IICPEX70 = x.IICPEX70,
                IICEX = x.IICEX,
                IDExamen = x.IDExamen
                //IDProfesor = x.IDProfesor 

            });

        return new GridResult
        {
            data = rows.ToArray(),
            count = table.Where(x => x.Asignaturas.Contains(name)).Count()
        };
    }

    public GridResult GetExamenGroupData(int page, string name)
    {

        var db = new DBTime2WinDataContext();
        {
            var tem = db.tbCursos.Where(o => o.Curso.IndexOf(name) != -1).Skip((page) * GRID_NO).Take(GRID_NO);
            return new GridResult
            {
                data = tem.ToArray(),
                count = tem.Where(x => x.Curso.IndexOf(name) != -1).Count()
            };
        }
    }

    internal object SaveData(string dataJson)
    {
        string data = dataJson;
        var lstCursos = new JavaScriptSerializer().Deserialize<List<tbCurso>>(data);
        if (lstCursos != null)
        {
            using (var db = new DBTime2WinDataContext())
            {
                foreach (var l in lstCursos)
                {
                    var temp = db.tbCursos.FirstOrDefault(o => o.IDCurso == l.IDCurso);
                    if (temp != null)
                    {
                        temp.IDNivel = l.IDNivel;
                        temp.MaxAdmitido = l.MaxAdmitido;
                        temp.NoOrder = l.NoOrder;
                        temp.Curso = l.Curso;
                    }
                }
                db.SubmitChanges();               
            }
            return new { status = "ok", message = "ok" };
        }
        return new { status = "fail", message = "fail" };
    }
}